package com.kalaqia.base.ui.list;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener;

import java.util.List;

/**
 * @author kalaqia 421407393@qq.com
 */
public abstract class BaseRefreshListFragment<T extends BaseListContract.Model,
        P extends BaseListContract.Presenter> extends BaseListFragment<T, P> {

    protected SmartRefreshLayout mRefreshLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_base_refresh;
    }

    @Override
    protected void initView(View view) {
        mRefreshLayout = view.findViewById(R.id.id_base_refresh_layout);
        mRecyclerView = new RecyclerView(view.getContext());
        mRefreshLayout.setRefreshContent(mRecyclerView);
        super.initView(view);
    }

    @Override
    public void setState(String msg) {
        super.setState(msg);
        mRefreshLayout.finishRefresh();
        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.finishLoadMore();
    }

    @Override
    protected void initRefreshLayout() {
        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.setDisableContentWhenRefresh(true);

        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                curPage++;
                if (onNetworkError()) {
                    curPage = 1;
                    mRefreshLayout.setEnableLoadMore(false);
                    return;
                }
                refreshData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                curPage = 1;
                if (onNetworkError()) {
                    return;
                }
                refreshData();
            }
        });
        mRefreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener() {
            @Override
            public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState,
                                       RefreshState newState) {
                if (newState == RefreshState.None) {
                    if (mOnRefreshDataListener != null) {
                        mOnRefreshDataListener.onRefresh();
                    }
                }
                super.onStateChanged(refreshLayout, oldState, newState);
            }
        });
        mRefreshLayout.autoRefresh();
    }

    private INetUtils netUtils;

    public void setNetUtils(INetUtils netUtils) {
        this.netUtils = netUtils;
    }

    protected boolean onNetworkError() {
        boolean result = netUtils == null || netUtils.isNetworkAvailable(getContext());
        if (!result && mStateUtils != null) {
            mStateUtils.showNetworkError();
            res = null;
            lists.clear();
            afterNoNetFinish();
        }
        return !result;
    }

    protected void afterNoNetFinish() {
        if (curPage == 1) {
            mRefreshLayout.finishRefresh();
        } else {
            mRefreshLayout.finishLoadMore();
        }
    }

    @Override
    public void showResult(List<T> results) {
        super.showResult(results);
        afterNoNetFinish();
    }

    @Override
    public void setAllPage(int page) {
        mRefreshLayout.setEnableLoadMore(curPage < page);
    }

    public void pullRefresh() {
        if (!mRefreshLayout.isRefreshing()) {
            mRefreshLayout.autoRefresh();
        }
    }

    private OnRefreshDataListener mOnRefreshDataListener;

    public void setOnRefreshDataListener(OnRefreshDataListener mOnRefreshDataListener) {
        this.mOnRefreshDataListener = mOnRefreshDataListener;
    }

    public interface OnRefreshDataListener {
        void onRefresh();
    }

}
